﻿using System.Net.Sockets;
using System.Net;
using System.Text;
using Vampirewal.Core.SocketCore;

namespace Test.SocketServer;

internal class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");

        Server2();
    }

    static void Server1()
    {
        // 创建一个用于监听的Socket
        Socket listener = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

        // 绑定IP地址和端口号
        IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
        IPEndPoint localEndPoint = new IPEndPoint(ipAddress, 12345);
        listener.Bind(localEndPoint);

        // 开始监听
        Console.WriteLine("Waiting for a connection...");
        listener.Listen(10);



        while (true)
        {
            // 接受连接
            Socket handler = listener.Accept();
            Console.WriteLine($"Connected: {handler.RemoteEndPoint}");

            try
            {
                while (true)
                {
                    // 读取数据
                    byte[] buffer = new byte[1024];
                    //int bytesRec = handler.Receive(buffer);

                    int bytesRec = 0;
                    StringBuilder data = new StringBuilder();

                    do
                    {
                        bytesRec = handler.Receive(buffer);
                        data.Append(Encoding.ASCII.GetString(buffer, 0, bytesRec));
                    }
                    while (handler.Available > 0);

                    if (bytesRec == 0)
                    {
                        // 客户端关闭连接
                        break;
                    }
                    string receivedData = data.ToString();
                    Console.WriteLine($"Received: {receivedData}");

                    // 发送数据
                    byte[] msg = Encoding.ASCII.GetBytes("Hello from server!");
                    handler.Send(msg);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Exception: {ex.Message}");
            }
            finally
            {
                if (!handler.Connected)
                {
                    // 关闭连接
                    handler.Shutdown(SocketShutdown.Both);
                    handler.Close();
                    Console.WriteLine("Connection closed");
                }

            }

        }
    }

    static void Server2()
    {
        TestViewModeel test = new TestViewModeel();

        var aaa= test.GetValue(TestViewModeel.MyPropertyProperty);

        test.SetValue(TestViewModeel.MyPropertyProperty, 123);
    }

}


public class TestViewModeel : DynamicObject
{


    public int MyProperty
    {
        get { return (int)GetValue(MyPropertyProperty); }
        set { SetValue(MyPropertyProperty, value); }
    }

    // Using a DependencyProperty as the backing store for MyProperty.  This enables animation, styling, binding, etc...
    public static readonly DynamicProperty<int> MyPropertyProperty = DynamicObject.Register<int>( "MyProperty", new DynamicPropertyMetadata<int>(11,callback));

    private static void callback(DynamicObject d, DynamicPropertyChangedEventArgs<int> e)
    {
        var aaa=Convert.ToInt32( e.NewValue);

        
    }
}